<script type="text/javascript">
	
	function findTotalTreatmentRecords()
	{
		<?php foreach($treatments as $e => $treatment) { ?>
				var total = 0;
				<?php foreach($branches as $b => $branch) { ?>
					if($('#TreatmentRecordValue_<?php echo $branch["Branch"]["id"]."_".$treatment["Treatment"]["id"] ?>').val().replace(",", "") != '')
						total += parseInt($('#TreatmentRecordValue_<?php echo $branch["Branch"]["id"]."_".$treatment["Treatment"]["id"] ?>').val().replace(",", ""));
				<?php } ?>
				$('#treatments_total_<?php echo $treatment["Treatment"]["id"] ?>').html($.formatNumber(total, {format:"#,###"}));
		<?php } ?>
	}

	jQuery(document).ready(function($) {
		<?php if($treatment_catid == 2) { ?>
			findTotalTreatmentRecords();
		<?php } ?>
		
		$("#treatment_record_frame input").blur(function(){
			findTotalTreatmentRecords();
		});		
	});

</script>

<div class="TreatmentRecords form" id="treatment_record_frame">
	<?php echo $this->render('_filter_add', '');?>
	
<?php echo $this->Form->create('TreatmentRecord');?>
	<fieldset>
		<legend><?php __('Thống kê các loại hình điều trị'); ?></legend>
	<?php
		echo $this->Form->input('year', array('label'=>'Năm', 'type'=>'hidden', 'value'=>$year));
		echo $this->Form->input('quarter', array('label'=>'Quý', 'type'=>'hidden', 'value'=>$quarter));
		echo $this->Form->input('city_id', array('label'=>'Tỉnh/Thành', 'type'=>'hidden', 'value'=>$city["id"]));
		echo $this->Form->input('unit_id', array('label'=>'Đơn vị', 'empty'=> true));
	?>	
	<input type="hidden" name="filter_treatment_catid" value="<?php echo $treatment_catid ?>"/>
	<div class="input text required">
		<label for="TreatmentRecordValue">Bảng cập nhật</label>
		
		<?php if($treatment_catid == 2) { ?>
		<div id="treatment_record_frame">
			<table cellpadding="0" cellpadding="0" id="input_table">
				<tr>
					<th></th>
					<?php foreach($branches as $b => $branch) { ?>
						<th><?php echo $branch["Branch"]["name"] ?></th>
					<?php } ?>
					<th>Tổng cộng</th>
				</tr>
				<?php foreach($treatments as $e => $treatment) { ?>
					<tr>
						<td><?php echo $treatment["Treatment"]["name"] ?></td>
						<?php foreach($branches as $b => $branch) { ?>
							<td>
								<input class="iit" type="text" id="TreatmentRecordValue_<?php echo $branch["Branch"]["id"]."_".$treatment["Treatment"]["id"] ?>" value="<?php if($values[$branch["Branch"]["id"]][$treatment["Treatment"]["id"]] != "") echo number_format($values[$branch["Branch"]["id"]][$treatment["Treatment"]["id"]],0,".", ",") ?>" maxlength="255" name="data[values][<?php echo $branch["Branch"]["id"] ?>][<?php echo $treatment["Treatment"]["id"] ?>]" />
							</td>
						<?php } ?>
						<td style="text-align:right" id="treatments_total_<?php echo $treatment["Treatment"]["id"] ?>"></td>
					</tr>
				<?php } ?>
			</table>
		</div>
		<?php } else { ?>
			<table cellpadding="0" cellpadding="0" id="input_tablez">
				<tr>
					<th></th>
					<th>Tổng số</th>
					<th>Ghi chú</th>
				</tr>
				<?php foreach($treatments as $e => $treatment) { ?>
					<tr>
						<td><?php echo $treatment["Treatment"]["name"] ?></td>
						<td>
							<input style="text-align: right" class="iit" type="text" id="TreatmentRecordTotal_<?php echo $treatment["Treatment"]["id"] ?>" value="<?php if($values['total'][$treatment["Treatment"]["id"]] != "") echo number_format($values['total'][$treatment["Treatment"]["id"]],0,".", ",") ?>" maxlength="255" name="data[total][<?php echo $treatment["Treatment"]["id"] ?>]" />
						</td>
						
						<td>
							<input class="iitz" type="text" id="TreatmentRecordNote_<?php echo $treatment["Treatment"]["id"] ?>" value="<?php echo $values['note'][$treatment["Treatment"]["id"]] ?>" maxlength="255" name="data[note][<?php echo $treatment["Treatment"]["id"] ?>]" />
						</td>
						
					</tr>
				<?php } ?>
			</table>
		<?php } ?>
		
	</div>
	</fieldset>
<?php echo $this->Form->end(__('Lưu thông tin', true));?>
</div>



<div class="actions">
	<h3><?php __('Chức năng'); ?></h3>
	<ul>
		<li><?php echo $this->Html->link(__('Danh sách đơn vị RHM', true), array('controller' => 'branches', 'action' => 'index')); ?> </li>
		<li><?php echo $this->Html->link(__('Thêm đơn vị RHM', true), array('controller' => 'branches', 'action' => 'add')); ?> </li>		
	</ul>
</div>